<?php
namespace Modules\Home\Controllers;

class GoodsLogController extends ControllerBase {
  public function initialize() {
    parent::initialize();

    $this -> _checkLogin();
    $this -> view -> setLayout('index');
  }

  public function indexAction() {
    $this -> tag -> prependTitle('货记录');

    $currentPage = abs($this -> request -> getQuery('page', 'int', 1));
    if($currentPage <= 0) {
      $currentPage = 1;
    }

    $sessionGetUser = $this -> session -> get('user');

    $builderGoodsLog = $this -> modelsManager -> createBuilder()
      -> from([
        'uwgl' => 'Common\\Models\\UserWalletGoodsLogModel'
      ])
      -> where('uwgl.user_id = ' . $sessionGetUser['user_id'])
      -> columns(
        'uwgl.user_wallet_goods_log_id, uwgl.type, uwgl.active, uwgl.is_draw, uwgl.draw_time, uwgl.create_time, uwgl.amount, uwgl.service_charge '
      )
      -> orderBy('uwgl.user_wallet_goods_log_id DESC');

    $type = $this -> request -> getQuery('type');
    if(isset($type) && !empty($type)) {
      $builderGoodsLog -> andWhere('uwgl.type in(' . $type . ')');
    }

    $pagerGoodsLog = new \Phalcon\Paginator\Pager(new \Phalcon\Paginator\Adapter\QueryBuilder([
      'builder' => $builderGoodsLog,
      'limit' => 10,
      'page' => $currentPage
    ]), [
      'layoutClass' => 'Modules\\Home\\Libraries\\Paginator\\Pager\\Layout\\Bootstrap',
      'rangeLength' => 5,
      'urlMask' => '?page={%page_number}'
    ]);

    $this -> view -> setVar('pagerGoodsLog', $pagerGoodsLog);
  }
}
?>